<?php

/**
 * Created by PhpStorm.
 * User: xiao
 * Date: 2017/6/13
 * Time: 上午10:41
 */
namespace app\src\filiation\logic;

use app\src\account\enmu\AccountEnmu;
use app\src\base\logic\BaseLogic;
use app\src\filiation\model\Filiation;
use app\src\invite\logic\InviteCodeLogic;
use app\src\platform\emnu\PlatformType;
use app\src\platform\logic\PlatformLogic;
use app\src\seller\logic\SellerLogic;

class FiliationLogic extends BaseLogic
{
    protected function _init(){
        $this->setModel(new Filiation());
    }

    /**
     * 添加用户父子关系根据邀请码
     * @param $uid
     * @param $invite_code
     */
    public function addByInviteCode($uid, $invite_code)
    {
        if($invite_code == 'itboye'){

            $entity = [ 'uid' => $uid ];

        }else{
            //验证查询邀请码所属
            $map = ['invite_code' => $invite_code];
            $result = (new InviteCodeLogic)->getInfo($map);
            if(!$result['status']){
                return $this->apiReturnErr($result['info']);
            }

            if(empty($result['info'])){
                //邀请码不存在，不做任何操作，返回真
                return $this->apiReturnSuc($result['info']);
            }

            $type = $result['info']['type'];
            $account_id = $result['info']['account_id'];

            $entity = [
                'uid' => $uid
            ];

            //删除旧的关系
            $map = ['uid' => $uid];
            $this->delete($map);

            $parents = [];

            switch ($type){
                case AccountEnmu::USER :
                    //在filiation表中查用户，没有就报错
                    $result = (new FiliationLogic)->getInfo(['uid' => $account_id]);
                    if(!$result['status']) return $this->apiReturnErr($result['info']);
                    if(empty($result['info'])) return $this->apiReturnErr('error: empty user');

                    $inviter_info = $result['info'];
                    $entity['p1'] = $inviter_info['uid'];
                    $entity['p1_5'] = $inviter_info['p1'];
                    $entity['p2'] = $inviter_info['p2'];
                    $entity['p3'] = $inviter_info['p3'];
                    $entity['p4'] = $inviter_info['p4'];
                    $entity['p5'] = $inviter_info['p5'];
                    break;
                case AccountEnmu::PLATFORM :
                    $result = (new PlatformLogic)->getPlatformInfo($account_id);
                    if(!$result['status']) return $this->apiReturnErr('error platform');
                    $platform_info = $result['info'];
                    $result = (new PlatformLogic)->getParents($account_id);
                    if(!$result['status']) return $this->apiReturnErr($result['info']);
                    if(empty($result['info'])) return $this->apiReturnErr('error: error platform');
                    $parents = $result['info'];
                    array_unshift($parents, $platform_info);
                    break;
                case AccountEnmu::SELLER :
                    $entity['p2'] = $account_id;
                    //查询商家所属平台
                    $result = (new SellerLogic)->getSellerInfo($account_id);
                    if(!$result['status']) return $this->apiReturnErr($result['info']);
                    if(empty($result['info'])) return $this->apiReturnErr('error: empty seller');
                    $parent_id = $result['info']['parent_id'];
                    $entity['p3'] = $parent_id;
                    $result = (new PlatformLogic)->getParents($parent_id);
                    if(!$result['status']) return $this->apiReturnErr($result['info']);
                    if(empty($result['info'])) return $this->apiReturnErr('error: error seller');
                    $parents = $result['info'];
            }

            if(in_array($type, [AccountEnmu::PLATFORM, AccountEnmu::SELLER])){
                foreach ($parents as $key => $platform){
                    if($platform['group_id'] == PlatformType::PERSONAL){
                        $entity['p3'] = $platform['id'];
                    }
                    if($platform['group_id'] == PlatformType::CITY_SERVICE){
                        $entity['p4'] = $platform['id'];
                    }
                    if($platform['group_id'] == PlatformType::PROVINCE_SERVICE){
                        $entity['p5'] = $platform['id'];
                    }
                }
            }
        }

        //写入数据
        $result = (new FiliationLogic)->add($entity);

        if($result['status']){
            return $this->apiReturnSuc('success');
        }else{
            return $this->apiReturnErr($result['info']);
        }
    }



    /**
     * 根据id查询自己的下属
     * account_id
     * type
     * field 查询等级
     */
    public function queryMembers($account_id){

        $map=['p2'=>$account_id];
        $order = 'f.uid,f.p2,m.realname,m.nickname,m.head,um.mobile';
        $query = $this->getModel();
        $list = $query->alias('f')
            ->join(['common_member'=>'m'],'m.uid=f.uid','left')
            ->join(['itboye_ucenter_member'=>'um'],'um.id=f.uid','left')
            ->where($map)
            ->field($order)
            ->select();


        return $this->apiReturnSuc($list);

    }

    /**
     * @param $uid
     * @param $type
     * @return array
     */

    public function queryMembersziji($uid, $type, $page = ['curpage'=>1, 'size'=>10]){
        $map=["$type"=>$uid];
        $order = 'f.uid,m.nickname,m.head,m.realname';
        $query = $this->getModel();
        $start = max(0,(intval($page['curpage'])-1)*intval($page['size']));
        $list = $query->alias('f')
            ->join(['common_member'=>'m'],'m.uid=f.uid','left')
            ->where($map)
            ->field($order)
            ->limit($start,$page['size'])
            ->select();
        $data = [];
        $count = $query->alias('f')
            ->join(['common_member'=>'m'],'m.uid=f.uid','left')
            ->where($map)
            ->count();
        foreach ($list as $vo){
            if(method_exists($vo,"toArray")){
                array_push($data,$vo->toArray());
            }
        }
        return $this -> apiReturnSuc(["count" => $count, "list" => $data]);

    }


}